package com.oa.ylms.apps.business.goods.impl;

import java.util.List;

import com.oa.ylms.apps.business.goods.IGoodsCheckService;
import com.oa.ylms.apps.business.goods.IGoodsInfoService;
import com.oa.ylms.apps.business.goods.IGoodsOrderDetailService;
import com.oa.ylms.apps.business.goods.IGoodsOrderService;
import com.oa.ylms.apps.business.plan.ICheckPlanService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.constants.ICheckStates;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.IPlanCategory;
import com.oa.ylms.apps.constants.account.IAccountInfoStates;
import com.oa.ylms.apps.constants.goods.IGoodsInfoStates;
import com.oa.ylms.apps.constants.IOrderStates;
import com.oa.ylms.apps.dao.goods.IGoodsOrderDao;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.goods.GoodCheckValue;
import com.oa.ylms.apps.entity.goods.GoodInfoValue;
import com.oa.ylms.apps.entity.goods.GoodOrderDetailValue;
import com.oa.ylms.apps.entity.goods.GoodOrderValue;
import com.oa.ylms.apps.entity.plan.CheckPlanValue;

public class GoodsOrderServiceImpl implements IGoodsOrderService {
	private IGoodsOrderDao orderDao;
	private IGoodsOrderDetailService detailService;
	private IGoodsCheckService checkService;
	private ICheckPlanService checkPlanService;
	private IGoodsInfoService infoService;
	private GoodInfoValue goodInfo;
	private GoodOrderValue goodOrder;
	private GoodCheckValue goodCheck;
	private GoodOrderDetailValue orderDetail;

	public void applyGoods(StaffInfoValue staffInfo,
			List<GoodOrderDetailValue> detailList) throws Exception {
		String []deptNo={};
		int postId = staffInfo.getArchiveInfo().getPostInfo().getPostId();
		CheckPlanValue plan = checkPlanService.queryByPostIdAndCategoryId(IPlanCategory.GOODSCHECK, postId);
		if(plan.getPlanOrders()!=null&&!plan.getPlanOrders().equals("")){
		 deptNo = plan.getPlanOrders().split(",");
		}
		if (deptNo.length != 0) {
			goodOrder.setStates(Integer.parseInt(deptNo[0]));
			goodOrder.setCheckPlan(plan);
			goodOrder.setStaffInfo(staffInfo);
			goodOrder.setCreateTime(DateUtil.toDate(DateUtil.getCurrentDate()));
			GoodOrderValue newOrder = orderDao.apply(goodOrder);
			if (detailList != null) {
				for (GoodOrderDetailValue od : detailList) {
					GoodInfoValue goodInfo = od.getGoodInfo();
					int remain = goodInfo.getTotal() - od.getQuantity();
					if (remain == 0) {
						goodInfo.setStates(IDEDStates.DISABLE);
					}
					goodInfo.setOnStore(remain);
					infoService.modifyGoodsInfo(goodInfo);
					od.setGoodOrder(newOrder);
					detailService.addOrderDetail(od);
				}

			}
		}
	}

	

	public void backGoods(List<GoodOrderDetailValue> detailList)
			throws Exception {
		GoodOrderValue goodOrder = queryById(detailList.get(0).getGoodOrder().getOrderId());
		goodOrder.setReturnTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		goodOrder.setStates(IOrderStates.RETURNED);
		
		for (GoodOrderDetailValue od : detailList) {
			int returnNum = od.getUseless();
			od = detailService.queryByOrderIdAndGoodId(infoService.queryByGoodId(od.getGoodInfo().getGoodId()), queryById(od.getGoodOrder().getOrderId()));
			GoodInfoValue goodInfo = infoService.queryByGoodId(od.getGoodInfo().getGoodId());
			goodInfo.setOnStore(goodInfo.getOnStore() + returnNum);
			goodInfo.setTotal(goodInfo.getTotal()-(od.getQuantity()-returnNum));
			if (returnNum != 0) {
				goodInfo.setStates(IDEDStates.ENABLE);
			}
			infoService.modifyGoodsInfo(goodInfo);
			od.setUseless(od.getQuantity()-returnNum);
			detailService.modifyOrderDetail(od);
			orderDao.modifyOrderState(goodOrder);
		}
	}

	public List<GoodOrderValue> queryPersonalOrder(StaffInfoValue staffInfo,
			int orderState, int rows, int pageNo) throws Exception {
		goodOrder.setStaffInfo(staffInfo);
		if (orderState != 0) {
			goodOrder.setStates(orderState);
		}
		return orderDao.queryPersonalOrder(goodOrder, rows, pageNo);
	}

	public int countTpages(StaffInfoValue staffInfo, int orderState, int rows)
			throws Exception {
		goodOrder.setStaffInfo(staffInfo);
		if (orderState != 0) {
			goodOrder.setStates(orderState);
		}
		return orderDao.countPersonalTpages(goodOrder, rows);
	}

	public List<GoodOrderValue> queryCheckOrder(StaffInfoValue staffInfo,
			int rows, int pageNo) throws Exception {
		int sectionId = staffInfo.getArchiveInfo().getPostInfo()
				.getSectionInfo().getSectionId();
		goodOrder.setStates(sectionId);
		return orderDao.queryCheckOrder(goodOrder, rows, pageNo);
	}

	public List<GoodOrderValue> queryCheckOrderByState(
			StaffInfoValue staffInfo, int isPassed, int rows, int pageNo)
			throws Exception {
		int sectionId = staffInfo.getArchiveInfo().getPostInfo()
				.getSectionInfo().getSectionId();
		goodCheck.setCheckOrder(sectionId);
		goodCheck.setIsPassed(isPassed);
		return orderDao.queryCheckOrderByState(goodCheck, rows, pageNo);
	}

	public List<GoodOrderDetailValue> queryDetailByOrderId(int orderId)
			throws Exception {
		goodOrder.setOrderId(orderId);
		return orderDao.queryDetailByOrder(goodOrder);
	}

	public GoodOrderValue queryById(int orderId) throws Exception {
		goodOrder.setOrderId(orderId);
		return orderDao.queryById(goodOrder);
	}

	public void checkPass(StaffInfoValue staffInfo, int orderId, String comments)
			throws Exception {
		goodOrder.setOrderId(orderId);
		GoodOrderValue order = orderDao.queryById(goodOrder);
		String planOrders = order.getCheckPlan().getPlanOrders();
		int states = order.getStates();
		int nextStates = checkPlanService.getStates(planOrders, states);
		order.setStates(nextStates);
		orderDao.modifyOrderState(order);
		goodCheck.setGoodOrder(order);
		goodCheck.setStaffInfo(staffInfo);
		goodCheck.setComments(comments);
		goodCheck.setCheckTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		goodCheck.setCheckOrder(states);
		goodCheck.setIsPassed(ICheckStates.CHECKPASSED);
		checkService.addGoodCheck(goodCheck);

	}

	public void checkUnpass(StaffInfoValue staffInfo, int orderId,
			String comments) throws Exception {
		goodOrder.setOrderId(orderId);
		GoodOrderValue order = orderDao.queryById(goodOrder);
		goodCheck.setGoodOrder(order);
		goodCheck.setStaffInfo(staffInfo);
		goodCheck.setComments(comments);
		goodCheck.setCheckTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		goodCheck.setCheckOrder(order.getStates());
		goodCheck.setIsPassed(ICheckStates.CHECKFAILED);
		checkService.addGoodCheck(goodCheck);
		order.setStates(IOrderStates.FAILED);
		List<GoodOrderDetailValue> detailList = detailService.queryByOrderId(order);
		for(GoodOrderDetailValue detail : detailList){
			goodInfo = infoService.queryByGoodId(detail.getGoodInfo().getGoodId());
			goodInfo.setOnStore(goodInfo.getOnStore() + detail.getQuantity());
			infoService.modifyGoodsInfo(goodInfo);
		}
		orderDao.modifyOrderState(order);
	}

	@Override
	public List<GoodOrderValue> queryReturnOrder() {

		return orderDao.queryReturnOrder();
	}

	public IGoodsOrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(IGoodsOrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public IGoodsOrderDetailService getDetailService() {
		return detailService;
	}

	public void setDetailService(IGoodsOrderDetailService detailService) {
		this.detailService = detailService;
	}

	public IGoodsCheckService getCheckService() {
		return checkService;
	}

	public void setCheckService(IGoodsCheckService checkService) {
		this.checkService = checkService;
	}

	public ICheckPlanService getCheckPlanService() {
		return checkPlanService;
	}

	public void setCheckPlanService(ICheckPlanService checkPlanService) {
		this.checkPlanService = checkPlanService;
	}

	public IGoodsInfoService getInfoService() {
		return infoService;
	}

	public void setInfoService(IGoodsInfoService infoService) {
		this.infoService = infoService;
	}

	public GoodOrderValue getGoodOrder() {
		return goodOrder;
	}

	public void setGoodOrder(GoodOrderValue goodOrder) {
		this.goodOrder = goodOrder;
	}

	public GoodCheckValue getGoodCheck() {
		return goodCheck;
	}

	public void setGoodCheck(GoodCheckValue goodCheck) {
		this.goodCheck = goodCheck;
	}

	@Override
	public int getTotalNumber() {
		return orderDao.getNumber();
	}



	public void setGoodInfo(GoodInfoValue goodInfo) {
		this.goodInfo = goodInfo;
	}



	public GoodInfoValue getGoodInfo() {
		return goodInfo;
	}



	public void setOrderDetail(GoodOrderDetailValue orderDetail) {
		this.orderDetail = orderDetail;
	}



	public GoodOrderDetailValue getOrderDetail() {
		return orderDetail;
	}

}
